package jnpf.scheduletask.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import jnpf.permission.OrganizeApi;
import jnpf.permission.entity.OrganizeEntity;
import jnpf.task.entity.TaskEntity;
import jnpf.task.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Component
public class synOrganizeHandler {
    @Autowired
    private TaskService taskService;
    @Autowired
    private OrganizeApi organizeApi;

    @XxlJob("synOrganizeHandler")
    @Transactional
    public void defaultHandler() {
        long time = System.currentTimeMillis();

        // 根据id 查询做数据处理

        TaskEntity info = taskService.getInfo("624588790995890821");

        String url = info.getUrl();

        String   date1= DateUtil.format(new Date(),  "yyyy-mm-dd") ;
        String   date2= DateUtil.format(new Date(),  "yyyy-mm-dd") ;
        if(info.getParam2()!=null){
            date1=info.getParam2();
        }

        String body="{\n" +
                "    \"parseRef\": {\n" +
                "        \"parseRefDictField\": false,\n" +
                "        \"parseRefOrdinaryField\": false,\n" +
                "        \"parseRefReferenceField\": true,\n" +
                "        \"replaceRefDictValue\": false,\n" +
                "        \"replaceRefOrdinaryValue\": false,\n" +
                "        \"parseRefWithSuffix\": \"__ref\"\n" +
                "    },\n" +
                "    \"args\": [\n" +
                "        {\n" +
                "            \"name\": \"M_DATA_STATE\",\n" +
                "            \"matchValues\": [\n" +
                "                \"0\",\n" +
                "                \"5\"\n" +
                "            ],\n" +
                "            \"matchType\": \"in\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\": \"M_LAST_MODIFIED_AT\",\n" +
                "            \"matchValues\": [\n" +
                "                \""+date1+" 00:00:00\",\n" +
                "                \""+date2+" 23:59:59\"\n" +
                "            ],\n" +
                "            \"matchType\": \"between\"\n" +
                "        }\n" +
                "\n" +
                "    ],\n" +
                "    \"sorts\": [],\n" +
                "    \"criteria\": {\n" +
                "        \"children\": [],\n" +
                "        \"matchType\": \"and\"\n" +
                "    },\n" +
                "    \"desensitization\": true\n" +
                "}";




        String post = HttpRequest.post(url)
                .header(Header.USER_AGENT, "Hutool http")//头信息，多个头信息多次调用此方法即可
                .header("AppKey",info.getParam1())
                .body(body )
                .timeout(120000)//超时，毫秒
                .execute().body();



        XxlJobHelper.log("请求返回的数据为：[{}]", post);

        // 对返回的数据进行解析

        JSONObject  jsonObject= new JSONObject(post);

         JSONArray jsonArray= (JSONArray) jsonObject.get("content");
         if(CollectionUtil.isNotEmpty(jsonArray)){

             for (int i = 0; i < jsonArray.size(); i++) {

                 JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                 String code = jsonObject1.getStr("code");



                 String M_DATA_STATE = jsonObject1.getStr("M_DATA_STATE");

                 if(!M_DATA_STATE.equals("5")){
                     OrganizeEntity infoByCode = organizeApi.getInfoByCode(code,"0");
                     if(infoByCode==null){

                         OrganizeEntity organizeEntity =new OrganizeEntity();

                         organizeEntity.setEnCode(code);
                         String parent_admin_org = jsonObject1.getStr("parent_admin_org");
                         if(parent_admin_org!=null){
                             OrganizeEntity parent = organizeApi.getInfoByCode("0",parent_admin_org);
                             if(parent!=null){
                                 organizeEntity.setParentId(parent.getId());
                             }

                         }
                         organizeEntity.setFullName(jsonObject1.getStr("name"));
                         organizeEntity.setOrgId(jsonObject1.getStr("org_id"));
                         organizeEntity.setEnName(jsonObject1.getStr("name_en_us"));
                         organizeEntity.setStatus(jsonObject1.getStr("status"));
                         if("广东宏石激光技术股份有限公司".equals(organizeEntity.getFullName())){
                             organizeEntity.setCategory("company");
                         }else {
                             organizeEntity.setCategory("department");
                         }
                         organizeApi.save(organizeEntity);

                     }else {
                         String parent_admin_org = jsonObject1.getStr("parent_admin_org");
                         if(parent_admin_org!=null){
                             OrganizeEntity parent = organizeApi.getInfoByCode("0",parent_admin_org);
                             if(parent!=null){
                                 infoByCode.setParentId(parent.getId());
                             }

                         }
                         infoByCode.setFullName(jsonObject1.getStr("name"));
                         infoByCode.setOrgId(jsonObject1.getStr("org_id"));
                         infoByCode.setEnName(jsonObject1.getStr("name_en_us"));
                         infoByCode.setStatus(jsonObject1.getStr("status"));
                         if("广东宏石激光技术股份有限公司".equals(infoByCode.getFullName())){
                             infoByCode.setCategory("company");
                         }else {
                             infoByCode.setCategory("department");
                         }
                         infoByCode.setSortCode(1L);
                         organizeApi.updateOrganizeEntity(infoByCode.getId(),infoByCode);



                     }
                 }



             }



         }





        System.out.println("执行了");
    }
}
